cap mkdir "$Rep_smokelabor/1_build/seer/proc"

** build county-level population file 
if 1 {
	
	** A copy of the raw data is provided in “$Rep_smokelabor/1_build/seer/raw/us.1969_2019.singleages.adjusted.exe”; 
	* 	decompress before executing the following scripts
	
	import delimited using "$Rep_smokelabor/1_build/seer/raw/us.1969_2019.singleages.adjusted.txt", clear
		
	gen year = substr(v1,1,4)
	destring year, replace
	drop if year<2006
	
	gen statefip = substr(v1,7,2)
	gen countycode = substr(v1,9,3)
	gen age = substr(v1,17,2)
	gen pop = substr(v1,19,8)
	
	drop v1
	
	destring statefip countycode age pop, replace
	gen countyfip = statefip*1000+countycode
	
	* Collapse to county*year*age (sum across sex, origin, and race)
	sort countyfip year age
	by countyfip year age: gen tot_pop = sum(pop)
	by countyfip year age: keep if _n==_N
	
	keep countyfip year age tot_pop
	
	* file 1: county-year level population for all-age, age16+, age60+, age60-
	preserve
	
		gen seer_pop 		= 1
		gen seer_pop16plus 	= (age>=16)
		gen seer_pop60plus 	= (age>=60)
		gen seer_pop60minus = (age<60)
		
		foreach measure in pop pop16plus pop60plus pop60minus {
			replace seer_`measure'=tot_pop*seer_`measure'
		}
		collapse (sum) seer_*, by(countyfip year)
		
		rename year rfrnc_yr
		order countyfip rfrnc_yr seer*
		
		lab var countyfip 		"FIPs county"
		lab var rfrnc_yr 		"Reference year"
		lab var seer_pop 		"SEER: population, all age"
		lab var seer_pop16plus 	"SEER: population, age 16+"
		lab var seer_pop60plus 	"SEER: population, age 60+"
		lab var seer_pop60minus "SEER: population, age 60-"
		
		compress
		saveold "$Rep_smokelabor/1_build/seer/proc/seer_county_year.dta", replace
		
	restore
	
	* file 2: stacked county-year-age population 
	* 	create age group label to match qwi age field: "https://lehd.ces.census.gov/pub/ak/latest_release/DVD-sa_f/label_agegrp.csv"
	* 	this will help merging later on
	preserve
	
		gen agegrp = "."
		replace agegrp = "A010203" 	if 14 <= age & age <= 24
		replace agegrp = "A04" 		if 25 <= age & age <= 34
		replace agegrp = "A05" 		if 35 <= age & age <= 44
		replace agegrp = "A06" 		if 45 <= age & age <= 54
		replace agegrp = "A07" 		if 55 <= age & age <= 64
		replace agegrp = "A08" 		if 65 <= age 
		drop if agegrp == "."
		
		collapse (sum) tot_pop, by(countyfip year agegrp)
		rename tot_pop seer_pop
		
		rename year rfrnc_yr
		order countyfip rfrnc_yr agegrp seer_pop
		
		lab var countyfip 		"FIPs county"
		lab var rfrnc_yr 		"Reference year"
		lab var agegrp 			"QWI age group code"
		lab var seer_pop 		"SEER: population in the age group"
		
		compress
		saveold "$SmokeLabor/rr_restat/to2019/seer/seer_agegrp_county_year.dta", replace
	
	restore
	
}

